package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service
@Slf4j
@Transactional
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    public PageResult findByPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        Integer page = setmealPageQueryDTO.getPage();
        Integer pageSize = setmealPageQueryDTO.getPageSize();

        PageHelper.startPage(page, pageSize);
        Page<SetmealVO> dish = setmealMapper.findPage(setmealPageQueryDTO);//后面 limit 0,10

        long total = dish.getTotal();//总记录数
        int pages = dish.getPages();//总页数
        List result = dish.getResult(); //本页数据

        //4.封装PageResultVo
        PageResult pageResultVo = new PageResult();
        //PageResult.setTotal(total);
        //PageResult.setRecords
        pageResultVo.setTotal(total);
        pageResultVo.setRecords(result);
        // Result re = new Result();
        return pageResultVo;

    }

    @Override
    public Integer changeStatus(Integer status, Long id) {
        if (status == StatusConstant.ENABLE) {
            setmealDishMapper.findDish(id).forEach(s -> {
                if (s.getStatus() == StatusConstant.DISABLE) {
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            });
        }
        return setmealMapper.changeStatus(status, id);
    }

    @Override
    public Integer delete(List<Long> ids) {
        setmealMapper.find(ids).forEach(s -> {
            if (s.getStatus() != StatusConstant.DISABLE)
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        });
       // setmealDishMapper.delete(ids);
        return setmealMapper.delete(ids);
    }

    @Override
    public SetmealVO findById(Long id) {
        return setmealMapper.findById(id);
    }

    @Override
    public Integer update(SetmealDTO setmeal) {
        Long id = setmeal.getId();
        //List<Long> ll = new ArrayList<>();
        setmeal.getSetmealDishes().forEach(s -> s.setSetmealId(id));
       /* setmealDishMapper.findDish(id).forEach(s -> {
          if (s.getStatus()==0){
              throw  new SetmealEnableFailedException(MessageConstant.UPDATEINFO_FAILED);
          }
        });*/
        List<Long> s = new ArrayList<>();
        s.add(id);
        setmealDishMapper.delete(s);
        setmealDishMapper.insert(setmeal.getSetmealDishes());
        Setmeal sm = new Setmeal();
        BeanUtils.copyProperties(setmeal, sm);
        sm.setStatus(StatusConstant.DISABLE);
        return setmealMapper.update(sm);

    }

    @Override
    public Integer add(SetmealDTO setmeal) {
        Setmeal sm = new Setmeal();
        BeanUtils.copyProperties(setmeal, sm);
        setmealMapper.insert(sm);
        setmeal.getSetmealDishes().forEach(s -> s.setSetmealId(sm.getId()));
        return setmealDishMapper.insert(setmeal.getSetmealDishes());
    }

    @Override
    public List<SetmealVO> findByCategoryId(Long categoryId) {
        return setmealMapper.findByCategoryId(categoryId);
    }

    @Override
    public List<DishItemVO> findBySetmealId(Long id) {

        return setmealDishMapper.findBySetmealId(id);
    }


}
